package com.jialiang.common.utils;

import com.jialiang.common.constants.BaseEnum;
import com.jialiang.common.exception.BusinessException;
import java.util.function.Supplier;

public class ThrowUtils {
    public ThrowUtils() {
    }

    public static void throwIf(boolean condition, RuntimeException runtimeException) {
        if (condition) {
            throw runtimeException;
        }
    }

    public static void throwIf(boolean condition, BaseEnum<?> errorCode) {
        throwIf(condition, (RuntimeException)(new BusinessException(errorCode)));
    }

    public static void throwIf(boolean condition, BaseEnum<?> errorCode, String message) {
        throwIf(condition, (RuntimeException)(new BusinessException(errorCode, message)));
    }

    public static ThrowBuilder builder() {
        return new ThrowBuilder();
    }

    public static class ThrowBuilder {
        public ThrowBuilder() {
        }

        public ThrowBuilder throwIf(boolean condition, Supplier<RuntimeException> runtimeException) {
            if (condition) {
                throw runtimeException.get();
            }
            return this;
        }

        public ThrowBuilder throwIf(boolean condition, BaseEnum<?> errorCode) {
            return this.throwIf(condition, () -> {
                return new BusinessException(errorCode);
            });
        }

        public ThrowBuilder throwIf(boolean condition, BaseEnum<?> errorCode, String message) {
            return this.throwIf(condition, () -> {
                return new BusinessException(errorCode, message);
            });
        }

        public void build() {
            // No need to do anything here since exceptions are thrown immediately in throwIf methods
        }
    }
}
